<?php
include_once("config.php");

function showView($view, $data) {
	echo parse("views/".$view.".php",$data);
}

$GLOBALS['BufferedErrors']=Array();
function errorParse($errno, $errstr, $errfile, $errline, $errcontext) {
    $errorTypes = Array(
        E_ERROR => 'Fatal Error',
        E_WARNING => 'Warning',
        E_PARSE => 'Parse Error',
        E_NOTICE => 'Notice',
        E_CORE_ERROR => 'Fatal Core Error',
        E_CORE_WARNING => 'Core Warning',
        E_COMPILE_ERROR => 'Compilation Error',
        E_COMPILE_WARNING => 'Compilation Warning',
        E_USER_ERROR => 'Triggered Error',
        E_USER_WARNING => 'Triggered Warning',
        E_USER_NOTICE => 'Triggered Notice',
        E_STRICT => 'Deprecation Notice',
        E_RECOVERABLE_ERROR => 'Catchable Fatal Error'
    );
    $ret=(object)Array(
        'number'=>$errno,
        'message'=>$errstr,
        'file'=>$errfile,
        'line'=>$errline,
        'context'=>$errcontext,
        'type'=>$errorTypes[$errno]
    );
    $GLOBALS['BufferedErrors'][]=$ret;
    return false;
}

function include_part($f,$p,$text,$argumentsToFile) {
    foreach ($argumentsToFile as $variableName => $variableValue)
        $$variableName = $variableValue;
	$tmp_fname = 'tmp/'.md5($f)."_".$p.".tmp";
	if (!file_exists($tmp_fname))
		file_put_contents($tmp_fname,$text);
	include $tmp_fname;
}

function parse($fileToInclude, $argumentsToFile=false) {
    $bufferedErrorStack = $GLOBALS['BufferedErrors'];
    set_error_handler('errorParse', error_reporting());
    $GLOBALS['BufferedErrors']=Array();
    if (!file_exists($fileToInclude))
        return '';
    if ($argumentsToFile === false)
        $argumentsToFile = Array();
    $af = $argumentsToFile;
    $argumentsToFile = array_merge($GLOBALS, $argumentsToFile);
    foreach ($argumentsToFile as $variableName => $variableValue)
        $$variableName = $variableValue;
    $fh = fopen($fileToInclude, 'r');
    $buffer = "";
    $mode = '';
    $global_pat = '';
    while (!feof($fh)) {
    	$line = fgets($fh);
    	$test = trim($line);
    	if (preg_match("/^\/\/(\w*)\/\/$/",$test,$name)) {
    		if ($mode=='') {
    			$global_pat = $buffer;
    		} elseif ($mode=='parent') {
    			$buffer = trim($buffer);
    			$ret = "views/".$buffer.".php";
    			unset($global_pat);
    		} else {
			    ob_start();
			    include_part($fileToInclude,$mode,$buffer,$argumentsToFile);
			    $$mode = trim(ob_get_contents());
			    $af[$mode] = $$mode;
			    ob_end_clean();
    		}
   			$mode = $name[1];
   			$buffer = '';
    	} else { $buffer .=$line."\n";}
    }
	if ($mode=='') {
		$global_pat = $buffer;
	} elseif ($mode=='parent') {
		$buffer = trim($buffer);
		$ret = "views/".$buffer.".php";
		unset($global_pat);
	} else {
	    ob_start();
	    include_part($fileToInclude,$mode,$buffer,$argumentsToFile);
	    $$mode = trim(ob_get_contents());
	    $af[$mode] = $$mode;
	    ob_end_clean();
	}

    if (isset($global_pat)) {
	    ob_start();
	    include_part($fileToInclude,'',$buffer,$argumentsToFile);
	    $ret = ob_get_contents();
	    ob_end_clean();
    } else $ret = parse($ret,$af);
    
    restore_error_handler();
    $errors = $GLOBALS['BufferedErrors'];
    $GLOBALS['BufferedErrors'] = $bufferedErrorStack;
    if (count($errors)>0) {
        $ret.='<ul class="error">';
        foreach ($errors as $error)
            $ret.= 
                '<li>'.
                    '<b>'.$error->type.'</b>: '.
                    $error->message.
                    '<blockquote>'.
                        '<i>file</i>: '.$error->file.'<br />'.
                        '<i>line</i>: '.$error->line.
                    '</blockquote>'.
                '</li>';
        $ret.='</ul>';
    }
    return $ret;
}
